﻿using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using CSLauncher.Configuration.Xml;

namespace CSLauncher
{
    internal class CSTrayIcon : IDisposable
    {
        public NotifyIcon Icon { get; private set; }

        public CSTrayIcon()
        {
            ContextMenu trayMenu = new ContextMenu();

            MenuItem editMenuItem = new MenuItem("Menu configuration");
            editMenuItem.Click += delegate { Process.Start(XmlConfiguration.Default); };
            trayMenu.MenuItems.Add(editMenuItem);
            
            MenuItem preferencesItem = new MenuItem("Preferences");
            preferencesItem.Click += delegate { PreferencesDialog.ShowSingle();};
            trayMenu.MenuItems.Add(preferencesItem);
            
            MenuItem exitItem = new MenuItem("Exit");
            exitItem.Click += delegate { Application.Exit(); };
            trayMenu.MenuItems.Add(exitItem);

            Assembly current = Assembly.GetExecutingAssembly();

            AssemblyConfigurationAttribute configuration =
                (AssemblyConfigurationAttribute)
                current.GetCustomAttributes(typeof (AssemblyConfigurationAttribute), true)[0];

            AssemblyFileVersionAttribute fileVersion = (AssemblyFileVersionAttribute)
                current.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true)[0];

            Icon = new NotifyIcon
                   {
                       ContextMenu = trayMenu,
                       Visible = true,
                       Icon = Properties.Resources.GenericIcon,
                       Text =
                           string.Format("CSLauncher v{0} {1}", fileVersion.Version, configuration.Configuration)
                   };
        }

        public void Dispose()
        {
            Icon.Dispose();
        }
    }
}
